package WebRoot.demo2.controller;

import java.util.ArrayList;
import java.util.List;

import org.hoyi.DB.CLuster.DataBaseCluster;
import org.hoyi.DB.Interface.IDatabase;
import org.hoyi.DB.model.DataTable;
import org.hoyi.DB.model.DbParameter;
import org.hoyi.dishop.Hoyipage;
import org.hoyi.wb.comment.PostParam;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;

import WebRoot.demo2.entity.user;

@RequestMode(MODE = { RequestType.GET, RequestType.POST })
public class UserController extends Hoyipage {

	// 增
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	@PostParam(ENTITY = user.class)
	public void addUser() {
		// hoyi 语法
		user u = this.getModelFromReq(user.class);
		int result = u.Insert();

//		 mysql 语法
//		 String sql = "insert into user (username,sex,note) values (?,?,?)";
//		 String[] params = {u.getUsername(),u.getSex(),u.getNote()};
//		
//		 int result=0;
//		 try {
//		 result = DataBaseCluster.Get_WRITE_SERVER().execUpdateEx(sql,
//		 params);
//		 } catch (SQLException e) {
//		 // TODO Auto-generated catch block
//		 e.printStackTrace();
//		 }
		 if (result==1) {
		 this.WriteUTF8JSONDATAMSG(1, "success", 0);
		 }else {
		 this.WriteUTF8JSONDATAMSG(0, "fail", 0);
		 }
	}

	// 删
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void delUser() {
		// hoyi 语法
		String id = this.getParams("id");

		int result = user.E().Where(user.id.Equals(id)).Delete();

//		String sql = "delete from user where id = ?";
//		String[] params = { id };
//
//		
//		 int result = 0;
//		 try {
//		 result = DataBaseCluster.Get_WRITE_SERVER().execUpdateEx(sql,
//		 params);
//		 } catch (SQLException e) {
//		 // TODO Auto-generated catch block
//		 e.printStackTrace();
//		 }

		if (result == 1) {
			this.WriteUTF8JSONDATAMSG(1, "success", 0);
		} else {
			this.WriteUTF8JSONDATAMSG(0, "fail", 0);
		}
	}

	// 查
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void findUser() {
		// hoyi 语法
		String id = this.getParams("id");
		// user u = user.E().Where(user.id.Equals(id)).First();
		// this.WriteUTF8JSONDATAMSG(1, "success", u);

		String sql = "select * from user where id = ?";
		List<DbParameter> params = new ArrayList<DbParameter>();
		params.add(new DbParameter("id", id));

		DataTable dt = DataBaseCluster.Get_READ_SERVER().ExecuteTableEx(sql,
				params);
		List<user> list = dt.toEntitys(user.class);
		if (list.size() > 0) {
			user u = list.get(0);
			this.WriteUTF8JSONDATAMSG(1, "success", u);
		} else {
			this.WriteUTF8JSONDATAMSG(1, "success", "not data");
		}

	}

	// 改
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void modifierUser() {
		// hoyi 语法
		String id = this.getParams("id");
		String username = this.getParams("username");
		user u = user.E().Where(user.id.Equals(id)).First();
		u.setUsername(username);
		int result = u.Update();

		// mysql 写法
		// StringBuilder sql = new StringBuilder();
		// sql.append("update user ");
		//
		// if (StringUtils.isNotBlank(username)) {
		// sql.append("set username = ?");
		// }
		//
		// sql.append("where id = ?");
		//
		// String[] params = {username,id};
		//
		// int result = 0;
		//
		// try {
		// result =
		// DataBaseCluster.Get_READ_SERVER().execUpdateEx(sql.toString(),
		// params);
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		if (result == 1) {
			this.WriteUTF8JSONDATAMSG(1, "success", "");
		} else {
			this.WriteUTF8JSONDATAMSG(0, "fail", "");
		}
	}

	/**
	 * 增（事务操作）
	 */
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void insertTransaction() {

		user u1 = new user("u1", "1", "note");
		user u2 = new user("u2", "1", "note");

		IDatabase database = DataBaseCluster.Get_Transaction_WRITE_SERVER();
		DataBaseCluster.Get_WRITE_SERVER();
		DataBaseCluster.Get_READ_SERVER();
		database.InitAndOpenTransaction();
		try {

			u1.Insert(database);
			int t = 1/0;  //设置一个异常，
			u2.Insert(database);

			database.Commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			database.RollBackTransaction(); //捕获异常，回滚
			e.printStackTrace();
		} finally {
			database.CloseConnection(); //关闭连接
		}

	}

}
